<template>
  <div class="user">
    <!-- 导航菜单 -->
    <el-menu
      :default-active="activeIndex"
      class="el-menu-demo"
      mode="horizontal"
      @select="handleSelect"
    >
      <el-menu-item index="1">用户管理</el-menu-item>
      <el-menu-item index="2">配置管理</el-menu-item>
    </el-menu>

    <!-- 用户管理 -->
    <div class="userManagement" v-if="isShow">
      <el-table :data="allUsersData" style="width: 100%">
        <el-table-column label="unid" width="180" align="center">
          <template v-slot="{ row }">
            {{ row.unid }}
          </template>
        </el-table-column>
        <el-table-column label="用户名" width="180" align="center">
          <template v-slot="{ row }">
            {{ row.username }}
          </template>
        </el-table-column>
        <el-table-column label="昵称" width="180" align="center">
          <template v-slot="{ row }">
            {{ row.nickname }}
          </template>
        </el-table-column>
        <el-table-column label="vip等级" width="180" align="center">
          <template v-slot="{ row }">
            {{ row.vipLevel }}
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center">
          <template v-slot="{ row }">
            <el-button
              size="mini"
              type="primary"
              v-has="'edit'"
              @click="editUser(row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              v-has="'delete'"
              @click="delUser(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- dialog -->
    <Dialog></Dialog>
  </div>
</template>
<script>
import * as api from "@/api/user";
import dialog from "@/components/userDialog";
import { mapMutations } from "vuex";
export default {
  components: {
    Dialog: dialog,
  },
  meta: {
    title: "yhgl",
    icon: "iconfont icon-yonghu",
  },
  data() {
    return {
      activeIndex: "1",
      allUsersData: [],
      isShow: true,
    };
  },
  methods: {
    handleSelect(key) {
      this.isShow = true;
      console.log(key);
      if (key == 2) {
        this.isShow = false;
      }
    },
    ...mapMutations(["SHOW_DIALOG", "SET_CURRENTUSER_ROW"]),
    editUser(row) {
      //显示dialog
      this.SHOW_DIALOG();
      console.log(row);
      //回显当前用户信息
      this.SET_CURRENTUSER_ROW(row);
    },
    delUser(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // console.log(row);
          // console.log(this.allUsersData);
          let target=this.allUsersData.filter(item=>item.unid!==row.unid)
          //console.log(target);
          this.allUsersData=target
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  created() {
    api.getAllUsers().then((res) => {
      //console.log(res);
      if (res.data.state) {
        this.allUsersData = res.data.data;
      } else {
        this.$message.error("获取全部用户信息失败");
      }
    });
  },
};
</script>
<style lang="less" scoped>
.user {
  margin-top: 20px;
}
</style>